﻿// See https://aka.ms/new-console-template for more information
using System.Diagnostics;

Watchdog();
static void Watchdog()
{
    while (true)
    {
        bool isProcessRunning = false;
        // 获取当前正在运行的所有进程，并查找 TestServer 进程
        Process[] processes = Process.GetProcessesByName("TestServer");
        if (processes.Length > 0)
        {
            isProcessRunning = true;
        }
        if (!isProcessRunning)
        {
            string executableName = File.ReadAllText("exec.txt");
            string normalizedPath = Path.GetFullPath(executableName);
            // 若 TestServer 进程不存在，启动该进程
            ProcessStartInfo psi = new ProcessStartInfo();

            psi.FileName = normalizedPath;
            psi.UseShellExecute = false;
            psi.CreateNoWindow = false;
            Process.Start(psi);
            Console.WriteLine("TestServer process restarted.");
        }
        // 等待 10 秒后进行下一次监测
        Thread.Sleep(60000);
    }
}